import 'package:flutter/material.dart';
import 'package:mtf/app/widget/TextCommon.dart';

@immutable
class AppBarLeftTitle extends StatelessWidget implements PreferredSizeWidget {
  final String? title;
  final bool? hasBack;
  final bool isWhiteBack;
  final String? right;
  final VoidCallback? onTap;
  final Widget? centerWidget;
  final Widget? leading;
  final Color? rightColor;
  final Widget? rightWidget;
  final Color? backgroundColor;
  final bool showDividerHorizontal;
  final PreferredSizeWidget? bottomWidget;
  final bool isCenterTitle;
  final double titleSize;
  final double height;

  AppBarLeftTitle(
      {super.key,
      this.backgroundColor,
      required this.title,
      this.isWhiteBack = true,
      this.hasBack,
      this.right,
      this.onTap,
      this.centerWidget,
      this.rightColor,
      this.rightWidget,
      this.showDividerHorizontal = true,
      this.leading,
      this.bottomWidget,
      this.isCenterTitle = true,
      this.titleSize = 18.0,
      this.height = 44.0});

  @override
  Widget build(BuildContext context) {
    bool canPop = ModalRoute.of(context)?.canPop ?? false;
    return AppBar(
      elevation: 0,
      titleSpacing: 0,
      centerTitle: isCenterTitle,
      backgroundColor: backgroundColor,
      actions: rightWidget != null
          ? [
              Padding(
                padding: EdgeInsets.only(right: 8),
                child: Center(
                  child: rightWidget,
                ),
              )
            ]
          : [
              GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: onTap,
                child: Align(
                  child: Padding(
                    padding: EdgeInsets.only(right: 16),
                    child: TextCommon(right ?? '',
                        color: rightColor ?? Colors.black),
                  ),
                ),
              )
            ],
      leading: canPop
          ? leading ??
              IconButton(
                  icon: Icon(
                    Icons.arrow_back_ios,
                    color: isWhiteBack ? Colors.white : Color(0xFF333333),
                    size: 22,
                  ),
                  onPressed: () => Navigator.pop(context))
          : Container(),
      title: TextCommon(
        title ?? '',
        size: titleSize,
        color: isWhiteBack ? Colors.white : Colors.black,
      ),
      bottom: this.bottomWidget,
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(height);
}
